/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlythuvien;

import thuvien.Account;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import thuvien.ThuVienInterface;

/**
 *
 * @author Tuyen
 */
public class QuanLyThuVien extends javax.swing.JFrame {

    /**
     * Creates new form QuanLyThuVien
     */
    public QuanLyThuVien() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu3 = new javax.swing.JMenu();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        lbStatus = new javax.swing.JLabel();
        btnDangNhap = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        lbUser = new javax.swing.JLabel();
        lbPass = new javax.swing.JLabel();
        txtusername = new javax.swing.JTextField();
        txtpass = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        mSys = new javax.swing.JMenu();
        mLogIn = new javax.swing.JMenuItem();
        mReg = new javax.swing.JMenuItem();
        mExit = new javax.swing.JMenuItem();
        mMuon_Tra = new javax.swing.JMenu();
        mMuon = new javax.swing.JMenuItem();
        mTra = new javax.swing.JMenuItem();
        mQlySach = new javax.swing.JMenu();
        mQlyMuon_Tra = new javax.swing.JMenu();
        mCaNhan = new javax.swing.JMenu();

        jMenu3.setText("jMenu3");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 102, 255));
        jLabel1.setText("Hệ thống quản lý thư viện");

        lbStatus.setText("Mời bạn đăng nhập để tiếp tục");

        btnDangNhap.setText("Đăng nhập");
        btnDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDangNhapActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        lbUser.setText("Tài khoản");

        lbPass.setText("Mật khẩu");

        mSys.setText("Hệ thống");

        mLogIn.setText("Đăng nhập");
        mLogIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mLogInActionPerformed(evt);
            }
        });
        mSys.add(mLogIn);

        mReg.setText("Đăng ký");
        mReg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mRegActionPerformed(evt);
            }
        });
        mSys.add(mReg);

        mExit.setText("Thoát");
        mExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mExitActionPerformed(evt);
            }
        });
        mSys.add(mExit);

        jMenuBar1.add(mSys);

        mMuon_Tra.setText("Mượn-trả sách");
        mMuon_Tra.setEnabled(false);

        mMuon.setText("Mượn Sách");
        mMuon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mMuonActionPerformed(evt);
            }
        });
        mMuon_Tra.add(mMuon);

        mTra.setText("Trả sách");
        mMuon_Tra.add(mTra);

        jMenuBar1.add(mMuon_Tra);

        mQlySach.setText("Quản lý sách");
        mQlySach.setEnabled(false);
        jMenuBar1.add(mQlySach);

        mQlyMuon_Tra.setText("Quản lý mượn-trả sách");
        mQlyMuon_Tra.setEnabled(false);
        jMenuBar1.add(mQlyMuon_Tra);

        mCaNhan.setText("Cá nhân");
        mCaNhan.setEnabled(false);
        jMenuBar1.add(mCaNhan);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(165, 165, 165)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(231, 231, 231)
                        .addComponent(lbStatus))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(137, 137, 137)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbPass)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbUser)
                                .addGap(29, 29, 29)
                                .addComponent(txtusername, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(258, 258, 258)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(172, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lbStatus)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbUser)
                    .addComponent(txtusername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPass)
                    .addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(btnDangNhap)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnThoat)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDangNhapActionPerformed
        try {
            String muser = txtusername.getText();
            String mpass = txtpass.getText();
            Account acc = lib.getAccount(muser);

            if (acc.getUsername().equalsIgnoreCase("") || !acc.getPass().equalsIgnoreCase(mpass)) {
                int dialog = JOptionPane.showConfirmDialog(this, "Bạn có muốn đăng ký", "Đăng nhập thất bại", JOptionPane.OK_CANCEL_OPTION);
                if (dialog ==0 ) {
                    DangKy reg = new DangKy();
                    reg.setVisible(true);
                }
            } else if (!muser.equalsIgnoreCase("admin") || !mpass.equalsIgnoreCase("admin")) {
                mCaNhan.setEnabled(true);
                mMuon_Tra.setEnabled(true);
            } else {
                mCaNhan.setEnabled(true);
                mMuon_Tra.setEnabled(true);
                mQlyMuon_Tra.setEnabled(true);
                mQlySach.setEnabled(true);
                txtusername.setVisible(false);
                txtpass.setVisible(false);
                btnDangNhap.setVisible(false);
                btnThoat.setVisible(false);
                lbPass.setVisible(false);
                lbUser.setVisible(false);
                lbStatus.setText("Đăng nhập thành công");
            }

        } catch (RemoteException ex) {
            Logger.getLogger(QuanLyThuVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnDangNhapActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        System.exit(0);
    }//GEN-LAST:event_btnThoatActionPerformed

    private void mRegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mRegActionPerformed
        DangKy reg = new DangKy();
        this.setVisible(false);
        reg.setVisible(true);
    }//GEN-LAST:event_mRegActionPerformed

    private void mExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mExitActionPerformed
        this.setVisible(false);
        System.exit(0);
    }//GEN-LAST:event_mExitActionPerformed

    private void mLogInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mLogInActionPerformed
        mCaNhan.setEnabled(false);
        mMuon_Tra.setEnabled(false);
        mQlyMuon_Tra.setEnabled(false);
        mQlySach.setEnabled(false);
        txtusername.setVisible(true);
        txtusername.setText("");
        txtpass.setText("");
        txtpass.setVisible(true);
        btnDangNhap.setVisible(true);
        btnThoat.setVisible(true);
        lbPass.setVisible(true);
        lbUser.setVisible(true);
        lbStatus.setText("Mời bạn đăng nhập để tiếp tục");
    }//GEN-LAST:event_mLogInActionPerformed

    private void mMuonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mMuonActionPerformed
        MuonSach muon=new MuonSach();
        this.setVisible(false);
        muon.setVisible(true);      
    }//GEN-LAST:event_mMuonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        try {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(QuanLyThuVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(QuanLyThuVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(QuanLyThuVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(QuanLyThuVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>

            /* Create and display the form */
            lib = (ThuVienInterface) Naming.lookup("rmi://localhost:1235/Nhom21");
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new QuanLyThuVien().setVisible(true);
                }
            });
        } catch (NotBoundException | MalformedURLException | RemoteException ex) {
            Logger.getLogger(QuanLyThuVien.class.getName()).log(Level.SEVERE, null, ex);
        }
        //</editor-fold>

        //</editor-fold>
    }
    public static ThuVienInterface lib;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDangNhap;
    private javax.swing.JButton btnThoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lbPass;
    private javax.swing.JLabel lbStatus;
    private javax.swing.JLabel lbUser;
    private javax.swing.JMenu mCaNhan;
    private javax.swing.JMenuItem mExit;
    private javax.swing.JMenuItem mLogIn;
    private javax.swing.JMenuItem mMuon;
    private javax.swing.JMenu mMuon_Tra;
    private javax.swing.JMenu mQlyMuon_Tra;
    private javax.swing.JMenu mQlySach;
    private javax.swing.JMenuItem mReg;
    private javax.swing.JMenu mSys;
    private javax.swing.JMenuItem mTra;
    private javax.swing.JTextField txtpass;
    private javax.swing.JTextField txtusername;
    // End of variables declaration//GEN-END:variables
}
